﻿using Microsoft.International.Converters.PinYinConverter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace PowerHub.MES.Common.NumberingCenter
{
    public static class GenerateBasicDataNO
    {
        /// <summary>
        /// 生成编号
        /// </summary>
        /// <param name="departmentName"></param>
        /// <returns></returns>
        public static string GenerateCode(string departmentName)
        {

            string guid = Guid.NewGuid().ToString();
            Random random = new Random(guid.GetHashCode());
            string randomNumber = random.Next(100, 1000).ToString();
            string randomNumber2 = random.Next(0, 10).ToString(); // 最后一位随机数

            // 如果输入包含数字，则返回默认部门编号以"D"开头
            if (Regex.IsMatch(departmentName, @"\d") || string.IsNullOrWhiteSpace(departmentName))
            {
                // 生成编号
                return $"D{GetCurrentTimeNumericPart()}-{randomNumber}-{randomNumber2}";
            }

            // 如果输入包含中文字符，则将中文转换为拼音
            if (Regex.IsMatch(departmentName, @"[\u4e00-\u9fa5]"))
            {
                ChineseChar chineseChar = new ChineseChar(departmentName[0]);
                string pinyin = chineseChar.Pinyins[0];
                string[] pinyinArray = pinyin.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                string code = "";
                foreach (var word in pinyinArray)
                {
                    code += word[0];
                }
                return $"{code.ToUpper()+ GetCurrentTimeNumericPart()}-{randomNumber}-{randomNumber2}" ;
            }

            // 如果输入为英文字母，则取部门名称的前三个字母作为部门编号
            return $"{departmentName.Substring(0, Math.Min(3, departmentName.Length)).ToUpper()}-{GetCurrentTimeNumericPart()}-{randomNumber2}";
        }

        public static string GenerateWorkOrderNO()
        {
            // 生成随机数部分
            string guid = Guid.NewGuid().ToString();
            Random random = new Random(guid.GetHashCode());
            string randomNumber = random.Next(100, 1000).ToString();
            string randomNumber2 = random.Next(0, 10).ToString(); // 最后一位随机数

            // 生成计量单位编号
            return $"WO{GetCurrentTimeNumericPart()}-{randomNumber}-{randomNumber2}";
        }

        public static string GenerateTaskNO()
        {
            // 生成日期部分
            string datePart = DateTimeOffset.UtcNow.ToString("yyMMdd");

            //获取Guid值作为随机数种子
            string guid = Guid.NewGuid().ToString();
            Random random = new Random(guid.GetHashCode());
            string IdPart = random.Next(10000, 99999).ToString();

            // 生成文档编号
            return $"Task{datePart}{IdPart}";
        }

        /// <summary>
        /// 生成计量单位编号
        /// </summary>
        /// <returns></returns>
        public static string GenerateUnitCode()
        {
            // 生成随机数部分
            string guid = Guid.NewGuid().ToString();
            Random random = new Random(guid.GetHashCode());
            string randomNumber = random.Next(100, 1000).ToString();
            string randomNumber2 = random.Next(0, 10).ToString(); // 最后一位随机数

            // 生成计量单位编号
            return $"{GenerateRandomString(3)}{GetCurrentTimeNumericPart()}-{randomNumber}-{randomNumber2}";
        }



        // 获取当前时间的数值部分
        private static string GetCurrentTimeNumericPart()
        {
            return DateTimeOffset.UtcNow.ToString("yyMMdd");
        }

        /// <summary>
        /// 生成随机字符串
        /// </summary>
        /// <param name="length">生成多少位</param>
        /// <returns></returns>
        private static string GenerateRandomString(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            Random random = new Random();
            return new string(Enumerable.Repeat(chars, length)
              .Select(s => s[random.Next(s.Length)]).ToArray());
        }

    }
}
